package com.lct.todo.servlet;

import com.google.gson.Gson;
import com.lct.todo.bean.TaskBean;
import com.lct.todo.daoimpl.TaskDaoImpl;
import com.lct.todo.response.TaskSelectAllResponse;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 查询任务数据
 */
public class SelectTaskServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //基本设置
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        PrintWriter printWriter = response.getWriter();
        //查询
        TaskSelectAllResponse selectAllResponse;
        List<TaskBean> taskBeanList = new TaskDaoImpl().findAll();
        if (taskBeanList == null) {
            selectAllResponse = new TaskSelectAllResponse(false, "系统异常", null);
        } else {
            selectAllResponse = new TaskSelectAllResponse(true, "", taskBeanList);
        }
        printWriter.println(new Gson().toJson(selectAllResponse));
        printWriter.flush();
        printWriter.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }
}
